<template>
    <!-- Content Wrapper. Contains page content -->
    <div class="content-wrapper">
      <!-- Content Header (Page header) -->
      <section class="content-header">
        <h1>
          系统管理
        </h1>

        <ol class="breadcrumb">
          <li><a href="#"> 控制台</a></li>
          <li><a href="#">角色管理</a></li>
          <li class="active">编辑</li>
        </ol>
      </section>

      <!-- Main content -->
      <section class="content">
          <div class="row">
            <div class="col-xs-12">
                <div class="box box-solid">
                  <div class="box-header with-border">
                    <h3 class="box-title">角色编辑</h3>
                  </div>
                  <!-- /.box-header -->
                  <!-- form start -->
                  <form class="form-horizontal">
                    <div class="box-body">
                      <div :class="['form-group',{'has-error':errors.has('name')}]">
                        <label class="col-sm-2 control-label">名称</label>
                        <div class="col-sm-4">
                          <input v-validate="'required|max:10'" name="name" class="form-control" placeholder="10个字" v-model="name">
                          <span class="help-block" v-if="errors.has('name')">{{errors.first('name')}}</span>
                        </div>
                      </div>
                    </div>
                    <!-- /.box-body -->
                    <div class="box-footer">
                      <button type="button" class="btn btn-primary" @click="submit">保存</button>
                    </div>
                    <!-- /.box-footer -->
                  </form>
                </div>
              <!-- /.box -->
            </div>
          </div>
      </section>
      <!-- /.content -->
    </div>
    <!-- /.content-wrapper -->
</template>

<script>
import Role from '@/modules/Role'

export default {
    mounted() {
        if(this.$route.params.id > 0){
            this.loadModel();
        }
    },

    data() {
        return {
            name:''
        };
    },

    methods: {

        loadModel() {
            Role.show(this.$route.params.id)
            .then((response) => {
                this.name = response.contents.name;
            });
        },

        submit() {
            this.$validator.validateAll().then((result) => {
                if(!result){
                    return;
                }
                var data = {
                    ...this.$data
                };

                var id = this.$route.params.id;

                if(id == 0){
                    Role.store(data)
                    .then((response) => {
                        $.message('操作成功');
                        this.$router.push({name:'RoleIndex'});
                    });
                }else{
                    Role.put(id,data)
                    .then((response) => {
                        $.message('操作成功');
                        this.$router.push({name:'RoleIndex'});
                    });
                }
            });
        }
    }
}
</script>
